//
//  ShakeDetailController.swift
//  FunSeeker
//
//  Created by 王泽宇 on 2017/4/4.
//  Copyright © 2017年 edu.Tongji.sse. All rights reserved.
//

import UIKit

class ShakeDetailController: UIViewController {
    var text = ""
    var actName = ""
    var itemInfo: AMapPOI?

    @IBOutlet weak var typeLabel: UILabel!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var backView: UIView!
    @IBOutlet weak var coreView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        coreView.layer.cornerRadius = 5;
        coreView.layer.shadowOpacity = 0.8
        coreView.layer.shadowColor = UIColor.black.cgColor
        coreView.layer.shadowOffset = CGSize(width: 2, height: 2)
        
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(quit))
        backView.addGestureRecognizer(recognizer)
        typeLabel.text = text
        nameLabel.text = itemInfo?.name
        if((itemInfo?.images.count)!>0){
            do {
                let data = try Data(contentsOf: URL(string: (itemInfo?.images[0].url)!)!)
                imageView.image = UIImage(data: data)
            } catch {
            }
        }
    }

    @IBAction func again(_ sender: UIButton) {
        doShake()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func quit() {
        self.dismiss(animated: true, completion: nil)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    @IBAction func ready(_ sender: UIButton) {
        goToNavi()
    }
    
    func goToNavi() {
        let shakeNVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ShakeNaviController") as! ShakeNaviController
        let upper = self.transitioningDelegate as! ShakeActionController
        shakeNVC.itemInfo = itemInfo
        self.dismiss(animated: true, completion: nil)
        upper.navigationController?.pushViewController(shakeNVC, animated: true)
    }
    
    func doShake() {
        let upper = self.transitioningDelegate as! ShakeActionController
        upper.nextItem()
        quit()
        upper.doShake()
    }
    
    //MARK: - shake related
    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        doShake()
    }
    
    override func becomeFirstResponder() -> Bool {
        return true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.resignFirstResponder()
        super.viewWillAppear(animated)
    }
}
